#include <QMessageBox>

#include "ivsmodel.h"



IVSModel::IVSModel(QObject *parent) :
    QObject(parent)
{
    if(readIVSIniFile())
        QMessageBox::warning(NULL,
                             tr("Error when reading IVS app settings"),
                             tr("Can not read IVS application settings.\nAccess error or malformat."),
                             QMessageBox::Ok);

    if(readVSIniFile() != 0)
        QMessageBox::warning(NULL,
                             tr("Error when reading video source setting"),
                             tr("Can not read video source settings.\nAccess error or malformat."),
                             QMessageBox::Ok);

    if(listVideoSource.length() > 0)
        currentVideoSourceIndex = 0;
    else
        currentVideoSourceIndex = -1;
    emit updateVideoSource();
    currentFunction = IVS::Video_player;
    isMakingROI = false;
}

void IVSModel::log(QString logContent)
{
    Logger::log(logContent);
}

int IVSModel::CurrentVideoSourceIndex(int index)
{
    if(index != -1)
        currentVideoSourceIndex = index;

    return currentVideoSourceIndex;
}

QStringList IVSModel::ListVideoSourceName()
{
    QStringList listVideoSourceName;
    foreach (IVS::VideoSource videoSource, listVideoSource) {
        listVideoSourceName.append(videoSource.sourceName);
    }
    return listVideoSourceName;
}

int IVSModel::addNewVideoSource(QString _videoSourceName)
{
    IVS::VideoSource videoSource = getDefaultVideoSource(_videoSourceName);
    if(!VideoSourceSetting::addVideoSource(videoSource))
        QMessageBox::warning(0,tr("Adding new video source"),
                             tr("This video source name had been tooken.\nPlease chose another name."),
                             QMessageBox::Ok);
    setCurrentVideoSource(_videoSourceName);

    return currentVideoSourceIndex;
}

IVS::VideoSource IVSModel::getDefaultVideoSource(QString _videoSourceName)
{
    IVS::VideoSource videoSource;
    videoSource.sourceName = _videoSourceName;
    videoSource.sourceType = IVS::LocalCAM;
    videoSource.source = "0";

    return videoSource;
}

void IVSModel::setCurrentFunction(IVS::eFunctions _currentFuntion)
{
    if(currentFunction != _currentFuntion){
        currentFunction = _currentFuntion;
        emit updateCurrentFunction();
    }
}

void IVSModel::setCurrentVideoSource(QString _currentVideoSourceName)
{
    for(int i = 0; i < listVideoSource.length(); i++){
        if(listVideoSource.at(i).sourceName.compare(_currentVideoSourceName) == 0)
        {
            currentVideoSourceIndex = i;
            qWarning(qPrintable(QString("cvs: %1").arg(currentVideoSourceIndex)));
            emit updateVideoSource();
            return;
        }
    }
}

IVS::VideoSource IVSModel::getCurrentVideoSource()
{
    if(currentVideoSourceIndex >=0)
        return listVideoSource.at(currentVideoSourceIndex);
    else
        return getEmptyVideoSource();
}

IVS::VideoSource IVSModel::CurrentVideoSource()
{
    if(currentVideoSourceIndex >=0)
        return listVideoSource[currentVideoSourceIndex];
    else
        return getEmptyVideoSource();
}

IVS::VideoSource IVSModel::getEmptyVideoSource()
{
    IVS::VideoSource videoSource;
    videoSource.sourceName = tr("");
    videoSource.sourceType = IVS::InvalidVideoSource;
    videoSource.source = tr("");

    return videoSource;
}

void IVSModel::setSource(QString _source)
{
    if(validateCurrentIndex())
        listVideoSource[currentVideoSourceIndex].source = _source;
}

bool IVSModel::validateCurrentIndex()
{
    return currentVideoSourceIndex >= 0 && currentVideoSourceIndex < listVideoSource.length();
}

void IVSModel::setSourceType(IVS::eVideoSourceType _videoSourceType)
{
    if(validateCurrentIndex())
        listVideoSource[currentVideoSourceIndex].sourceType = _videoSourceType;
}

void IVSModel::setROI(QList<CvPoint> _ROI)
{
    if(validateCurrentIndex())
        listVideoSource[currentVideoSourceIndex].ROI = _ROI;
}

void IVSModel::saveAllSettings()
{
    if(writeIVSIniFile() != 0)
        QMessageBox::warning(NULL, tr("Save IVS settings error"), tr("Write IVS Setting with error"), QMessageBox::Ok);

    if(writeVSIniFile() != 0)
        QMessageBox::warning(NULL, tr("save video source settings error"), tr("Write video source settings with error!"), QMessageBox::Ok);
}

void IVSModel::setVerboseLevel(QString _verboseLevelText)
{
    int _verboseLevel = _verboseLevelText.toInt();
    if(_verboseLevel >=0 &&verboseLevel <= 3)
        verboseLevel = _verboseLevel;
}

IVS::eFunctions IVSModel::CurrentFunction()
{
    return currentFunction;
}

void IVSModel::setVideoPlayerSettings_EnableROI(bool _isEnableROI)
{
    video_player_settings.isEnableROI = _isEnableROI;
    emit updateCurrentFunctionSettings();
}

IVSSettings::Video_player_settings IVSModel::VideoPlayerSettings()
{
    return video_player_settings;
}

bool IVSModel::toogleMakingROI()
{
    isMakingROI = !isMakingROI;
    if(isMakingROI){
        listVideoSource[currentVideoSourceIndex].ROI.clear();
        emit updateROI(true);
    }else
        emit updateROI(false);

    return isMakingROI;
}

bool IVSModel::IsMakingROI()
{
    return isMakingROI;
}

void IVSModel::ROI_add_point(int x, int y)
{
    if(isMakingROI){
        listVideoSource[currentVideoSourceIndex].ROI.append(cvPoint(x,y));
        emit updateROI(false);
    }
}

IVSSettings::Intruders_detection_settings IVSModel::IntrudersDetectionSettings()
{
    return intruders_detection_settings;
}

void IVSModel::setIntrudersDetectionSettings_NoFrameToInitBG(int nFrame)
{
    intruders_detection_settings.noFrameToInitBackground = nFrame;
    emit updateCurrentFunctionSettings();
}

void IVSModel::setIntrudersDetectionSettings_Alpha(float alpha)
{
    intruders_detection_settings.alpha = alpha;
    emit updateCurrentFunctionSettings();
}

void IVSModel::setIntrudersDetectionSettings_StateChangeDelay(int nFrame)
{
    intruders_detection_settings.stateChangeDelay = nFrame;
    emit updateCurrentFunctionSettings();
}

void IVSModel::setIntrudersDetectionSettings_BoundRectAreaMin(int bnRectAreaMin)
{
    intruders_detection_settings.bnRectArea_min = bnRectAreaMin;
    emit updateCurrentFunctionSettings();
}

void IVSModel::setIntrudersDetectionSettings_BoundRectAreaMax(int bnRectAreaMax)
{
    intruders_detection_settings.bnRectArea_max = bnRectAreaMax;
    emit updateCurrentFunctionSettings();
}

void IVSModel::setIntrudersDetectionSettings_NoBin(int noBin)
{
    intruders_detection_settings.noBin = noBin;
    emit updateCurrentFunctionSettings();
}

IVSSettings::Restricted_area_survuillance_settings IVSModel::RestrictedAreaSurvuillanceSettings()
{
    return restricted_area_survuillance_settings;
}

void IVSModel::setRestrictedAreaSurvuillanceSettings_NoFrameToInitBG(int nFrame)
{
    restricted_area_survuillance_settings.noFrameToInitBackground = nFrame;
    emit updateCurrentFunctionSettings();
}

void IVSModel::setRestrictedAreaSurvuillanceSettings_Alpha(float alpha)
{
    restricted_area_survuillance_settings.alpha = alpha;
    emit updateCurrentFunctionSettings();
}

void IVSModel::setRestrictedAreaSurvuillanceSettings_StateChangeDelay(int nFrame)
{
    restricted_area_survuillance_settings.stateChangeDelay = nFrame;
    emit updateCurrentFunctionSettings();
}

void IVSModel::setRestrictedAreaSurvuillanceSettings_BoundRectAreaMin(int bnRectAreaMin)
{
    restricted_area_survuillance_settings.bnRectArea_min = bnRectAreaMin;
    emit updateCurrentFunctionSettings();
}

void IVSModel::setRestrictedAreaSurvuillanceSettings_BoundRectAreaMax(int bnRectAreaMax)
{
    restricted_area_survuillance_settings.bnRectArea_max = bnRectAreaMax;
    emit updateCurrentFunctionSettings();
}

void IVSModel::setRestrictedAreaSurvuillanceSettings_NoBin(int noBin)
{
    restricted_area_survuillance_settings.noBin = noBin;
    emit updateCurrentFunctionSettings();
}

IVSSettings::Camera_checks_settings IVSModel::CameraChecksSettings()
{
    return camera_checks_settings;
}

void IVSModel::setCameraChecksSettings_NoFrameToInitBG(int nFrame)
{
    camera_checks_settings.noFrameToInitBackground = nFrame;
}

void IVSModel::setCameraChecksSettings_Apla(float alpha)
{
    camera_checks_settings.alpha = alpha;
}

void IVSModel::setCameraChecksSettings_StateChangeDelay(int nFrame)
{
    camera_checks_settings.stateChangeDelay = nFrame;
}

void IVSModel::setCameraChecksSettings_VideoPlaySpeedBoost(int fps)
{
    camera_checks_settings.videoPlaySpeedBoost = fps;
}

void IVSModel::setCameraChecksSettings_BeCoveredThreshold(int illumination)
{
    camera_checks_settings.beCoveredThreshold = illumination;
}
